//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.export;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.jpgolay.me.persistence.OLD_RequestDB;
import org.jpgolay.me.util.MEDate;

public class OLD_ExportCRUD {

    public OLD_ExportCRUD() {
        resultat = false;
        continu = false;
        dateDebut = "";
        dateFin = "";
        destination = "";
        userId = "";
        dbr = new OLD_RequestDB();
    }

    public boolean factureEBP() {
        exportFactEBP();
        return resultat;
    }

    public boolean factureCIEL() {
        exportFactCIEL();
        return resultat;
    }

    public boolean factureSARI() {
        exportFactSARI();
        return resultat;
    }

    public boolean factureJCOMPTA() {
        exportFactJCOMPTA();
        return resultat;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setDateDebut(String dateDebut) {
        this.dateDebut = dateDebut;
    }

    public void setDateFin(String dateFin) {
        this.dateFin = dateFin;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    private void exportFactEBP() {
        String export = "";
        MEDate d = new MEDate();
        String debut = d.toMysql(dateDebut);
        String fin = d.toMysql(dateFin);
        String dateDuJour = d.getEBP();
        String requete = "select date_format( date, '%d%m%y'), id, net_a_payer, total_taxes, total_brut_ht, clients_id from ventes_factures where date between '" + debut + "' and '" + fin + "' order by id";
        dbr.open();
        try {
            jeuResultats = dbr.getTable(requete);
            if (jeuResultats.next()) {
                int c = 1;
                do {
                    export = export + c + "," + jeuResultats.getString(1) + ",VE,411" + jeuResultats.getString(6) + ",,\"Facture N FC" + jeuResultats.getString(2) + "\",\" FC" + jeuResultats.getString(2) + "\"," + jeuResultats.getString(3) + ",D," + jeuResultats.getString(1) + ",EUR\n";
                    c++;
                    export = export + c + "," + jeuResultats.getString(1) + ",VE,4457,,\"Facture N FC" + jeuResultats.getString(2) + "\",\" FC" + jeuResultats.getString(2) + "\"," + jeuResultats.getString(4) + ",C," + dateDuJour + ",EUR\n";
                    c++;
                    export = export + c + "," + jeuResultats.getString(1) + ",VE,707,,\"Facture N FC" + jeuResultats.getString(2) + "\",\" FC" + jeuResultats.getString(2) + "\"," + jeuResultats.getString(5) + ",C," + dateDuJour + ",EUR\n";
                    c++;
                } while (jeuResultats.next());
                continu = true;
            } else {
                resultat = false;
                continu = false;
            }
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            resultat = false;
            continu = false;
        }
        dbr.close();
        if (!export.equals("") && continu) {
            try {
                PrintWriter write = new PrintWriter(new BufferedOutputStream(new FileOutputStream(destination + ".txt")));
                write.write(export);
                write.close();
                resultat = true;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                resultat = false;
            }
        }
    }

    private void exportFactCIEL() {
    }

    private void exportFactSARI() {
    }

    private void exportFactJCOMPTA() {
    }
    private OLD_RequestDB dbr;
    private ResultSet jeuResultats;
    private boolean resultat;
    private boolean continu;
    private String dateDebut;
    private String dateFin;
    private String destination;
    private String userId;
}
